//
// Copyright (C) OpenSim Ltd.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, see <http://www.gnu.org/licenses/>.
//


#include "Router.h"

namespace inet {

Define_Module(Router);
// 添加ACL表路由项
void Router::addACLEntry(ACLEntry ae){
    ACLTable.push_back(ae);
}

void Router::handleMessage(omnetpp::cMessage *msg) {
    if (msg->arrivedOn("controlPort$i")) {
        // from configuration_port
        std::cout << "routers receive configuration information" << "\n";


        ACLSingleEntry *a_pk2 = dynamic_cast<ACLSingleEntry *>(msg);
        if(a_pk2 != nullptr){
            ACLEntry ae;
            ae.SourceAddress = inet::Ipv4Address(192,168,(a_pk2->getSourceAddressInt() +1) / 256,(a_pk2->getSourceAddressInt() +1) % 256);
            ae.DestAddress = inet::Ipv4Address(192,168, (a_pk2->getDestAddress() +1) / 256, (a_pk2->getDestAddress() +1) % 256);
            ae.NextHopAddress = inet::Ipv4Address(a_pk2->getNextHopAddress());
            ae.SourcePort = a_pk2->getSourcePort();
            ae.interfaceID = a_pk2->getInterfaceID();
            ae.priority = a_pk2->getPriority();
            ae.protocolID = a_pk2->getProtocolID();
            entryToForward = ae;
            addACLEntry(ae);
            needReadACLTable = true;
             EV<<"SourceAddress in ACL"<< ae.SourceAddress<<endl;
            EV<<"DestAddress in ACL"<<  ae.DestAddress<<endl;
             EV<<"NextHopAddress IDin ACL"<< ae.NextHopAddress <<endl;
            EV<<"NextHopAddress in ACL"<<  inet::Ipv4Address(a_pk2->getNextHopAddress())<<endl;
            EV<<"SourcePort in ACL"<<  a_pk2->getSourcePort()<<endl;
            EV<<"interfaceID in ACL"<<  a_pk2->getInterfaceID()<<endl;
        }
        delete a_pk2;
    }
    //TODO handle message from controller needReadACLTable
    else
        throw omnetpp::cRuntimeError("Unknown gate");
    }

}   // namespace inet

